<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="绿化区域" prop="greenAreaId">
        <el-select
          clearable
          v-model="queryParams.greenAreaId"
          placeholder="请选择绿化区域"
        >
          <el-option
            v-for="item in buiGreenAreaListAll"
            :key="item.id"
            :label="item.areaName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="园区" prop="parkId">
        <el-select
          clearable
          v-model="queryParams.parkId"
          @change="getBuildingList(queryParams.parkId)"
          placeholder="请选择园区"
        >
          <el-option
            v-for="item in buiParkList"
            :key="item.id"
            :label="item.parkNum"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="责任人" prop="liableId">
        <el-select
          clearable
          v-model="queryParams.liableId"
          placeholder="请选择绿化区域"
        >
          <el-option
            v-for="item in wyUserList"
            :key="item.id"
            :label="item.userName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item> -->

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['park:wyGreenManagement:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['park:wyGreenManagement:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['park:wyGreenManagement:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['park:wyGreenManagement:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="wyGreenManagementList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="绿化区域" align="center" prop="areaName" />
      <el-table-column label="园区" align="center" prop="parkNum" />
      <el-table-column
        label="计划安排"
        align="center"
        prop="planTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="责任人" align="center" prop="userName" />
      <el-table-column label="执行结果" align="center" prop="result" >
        <template slot-scope="scope">
          <div v-for="(item,index) in postCondition" :key="index">
            <span v-if="scope.row.result==item.value">{{ item.label}}</span>
            </div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['park:wyGreenManagement:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['park:wyGreenManagement:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改绿化管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="园区" prop="parkId">
          <el-select
            clearable
            v-model="form.parkId"
            @change="getBuildingList(form.parkId)"
            placeholder="请选择园区"
          >
            <el-option
              v-for="item in buiParkList"
              :key="item.id"
              :label="item.parkNum"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="绿化区域" prop="greenAreaId">
          <el-select
            clearable
            v-model="form.greenAreaId"
            placeholder="请选择绿化区域"
          >
            <el-option
              v-for="item in buiGreenAreaList"
              :key="item.id"
              :label="item.areaName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="计划安排" prop="planTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.planTime"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择计划安排"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="责任人" prop="liableId">
          <el-select
            clearable
            v-model="form.liableId"
            placeholder="请选择责任人"
          >
            <el-option
              v-for="item in wyUserList"
              :key="item.id"
              :label="item.userName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="领料" prop="requisitionDetailList">
          <div
            class="requisitionDetailList"
            v-for="(item, index) in form.requisitionDetailList"
            :key="index"
          >
            <span>名称</span>
            <el-input v-model="item.materialName" />
            <span>数量</span>
            <el-input v-model.number="item.quantity" type="number" />
            <i
              v-if="index === 0"
              @click="addfacility"
              class="el-icon-circle-plus-outline"
            ></i>
            <i
              v-else
              @click="delfacility(index)"
              class="el-icon-remove-outline"
            ></i>
          </div>
        </el-form-item>
        <el-form-item label="执行结果" prop="result">
          <el-select v-model="form.result" placeholder="请选择执行结果">
          <el-option
            v-for="item in postCondition"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWyUser } from "@/api/smartParks/property/propertyGreeningPeople";

import {
  listWyGreenManagement,
  getWyGreenManagement,
  delWyGreenManagement,
  addWyGreenManagement,
  updateWyGreenManagement,
} from "@/api/smartParks/property/propertyGreening";
import { listBuiPark } from "@/api/parkAssets/enterCommunity";
import { listBuiGreenArea } from "@/api/parkAssets/buildingGreening";
export default {
  name: "WyGreenManagement",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 绿化管理表格数据
      wyGreenManagementList: [],
      // 弹出层标题
      title: "",
      buiParkList: [],
      buiGreenAreaList: [],
      buiGreenAreaListAll: [],
      wyUserList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        greenAreaId: null,
        parkId: null,
        planTime: null,
        liableId: null,
        material: null,
        result: null,
      },
      // 表单参数
      form: {
      },
      // 执行结果
      postCondition:[
        {
          label:'未开始',
          value:'0'
        },
        {
          label:'执行中',
          value:'1'
        },
        {
          label:'已完成',
          value:'2'
        }
      ],
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    listBuiPark().then((response) => {
      this.buiParkList = response.rows;
    });
    listWyUser({ userType: '绿化' }).then((response) => {
      this.wyUserList = response.rows;

    });
    listBuiGreenArea().then((response) => {
      this.buiGreenAreaListAll = response.rows;
    });
  },
  methods: {
    addfacility() {
      this.form.requisitionDetailList.push({
        materialName: null,
        quantity: null,
      });
    },
    delfacility(index) {
      this.form.requisitionDetailList.splice(index, 1);
    },
    //得到楼栋
    getBuildingList(value) {
      this.form.greenAreaId = "";
      listBuiGreenArea({ parkId: value }).then((response) => {
        this.buiGreenAreaList = response.rows;
      });
    },
    /** 查询绿化管理列表 */
    getList() {
      this.loading = true;
      listWyGreenManagement(this.queryParams).then((response) => {
        this.wyGreenManagementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        greenAreaId: null,
        parkId: null,
        planTime: null,
        liableId: null,
        material: null,
        result: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        requisitionDetailList: [
          {
            materialName: null,
            quantity: null,
          }
        ]
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加绿化管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getWyGreenManagement(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改绿化管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateWyGreenManagement(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWyGreenManagement(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除绿化管理的数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delWyGreenManagement(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "park/wyGreenManagement/export",
        {
          ...this.queryParams,
        },
        `park_wyGreenManagement.xlsx`
      );
    },
  },
};
</script>
<style scoped lang='scss'>
.requisitionDetailList {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  span {
    flex-shrink: 0;
    margin-left: 10px;
  }
  .el-input {
    margin-left: 10px;
  }
  i {
    font-size: 20px;
    margin-left: 10px;
    cursor: pointer;
  }
}
</style>
